package org.hawk.gwt.ppc.parser.impl;

import java.util.Iterator;

import org.hawk.gwt.ppc.parser.NoSuchTokenException;
import org.hawk.gwt.ppc.parser.Token;
import org.hawk.gwt.ppc.utils.PreprocessorException;

/**
 * Token source that reads tokens from {@link Iterator}.
 * 
 * @author alex.bereznevatiy@gmail.com
 *
 */
final class IteratorTokenSource extends AbstractTokenSource implements TokenSource {
	
	private Iterator<Token> iterator;
	
	/**
	 * Creates IteratorTokenSource based on iterator passed.
	 * Iterator state can be available to client.
	 * 
	 * @param iterator
	 */
	IteratorTokenSource(Iterator<Token> iterator) {
		this.iterator = iterator;
	}

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.parser.impl.TokenSource#current()
	 */
	public Token current() {
		if (current == null) {
			throw new NoSuchTokenException();
		}
		return current;
	}

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.parser.impl.TokenSource#hasNext()
	 */
	public boolean hasNext() {
		return iterator.hasNext();
	}

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.parser.impl.TokenSource#next()
	 */
	public Token next() throws PreprocessorException {
		if (iterator.hasNext()) {
			return current = iterator.next();
		}
		throw new NoSuchTokenException();
	}
}
